import java.io.*;
import java.util.*;

public class Main {
	BufferedReader bf;
	StringTokenizer st;
	StringBuilder sb;
	
	int n,
		r,
		c;
	char[][] graph;
	
	public Main() {
		bf = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
	}
	
	public static void main(String[] args) throws IOException {
		Main main = new Main();
		main.beginProcess();
	}

	public void beginProcess() throws IOException {
		try {
			int i, j;
			String input;
			while((input = bf.readLine()) != null) {
				n = Integer.parseInt(input);
				while(n > 0) {
					st = new StringTokenizer(bf.readLine());
					r = Integer.parseInt(st.nextToken());
					c = Integer.parseInt(st.nextToken());
					graph = new char[r][c];
					for(i = 0; i < r; i++) {
						st = new StringTokenizer(bf.readLine());
						for(j = 0; j < c; j++)
							graph[i][j] = st.nextToken().charAt(0);
					}

					determineEdge();
					if(n != 1) sb.append("\n");
					
					n--;
				}
			}
			System.out.print(sb);
		}
		catch(Exception ex) {
			System.err.println(ex.toString());
		}
	}
	
	private void determineEdge() {
		int i,
			j;
		boolean isEdge;
		for(i = 0; i < r; i++) {
			for(j = 0; j < c; j++)
				if(graph[i][j] == '0') {
					sb.append("_ ");
				} else {
					isEdge = false;
					if(i - 1 >= 0 && graph[i - 1][j] == '0') isEdge = true;
					else if(i + 1 < r && graph[i + 1][j] == '0') isEdge = true;
					else if(j - 1 >= 0 && graph[i][j - 1] == '0') isEdge = true;
					else if(j + 1 < c && graph[i][j + 1] == '0') isEdge = true;
					
					if(isEdge) sb.append("0 ");
					else sb.append("_ ");
				}
			sb.append("\n");
		}
	}
	
}